package com.mengtian_tech.tju.dataquery.api.service;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

/**
 * 车辆备案数据
 */
@Service
public class CarVisitApplyService {
	
	@Autowired
	@Qualifier("jdbcTemplateDataAi")
	JdbcTemplate jdbcTemplateDataAi;
	

	@Autowired
	@Qualifier("jdbcTemplateCarEnter")
	JdbcTemplate jdbcTemplateCarEnter;
	
	/**
	 * 同步大客车
	 */
	public synchronized int asyncBusApply() {
		//查询条件
		String where = "";
		
		//首先获取最近的同步信息
		List<Map<String,Object>> asyncList = this.jdbcTemplateDataAi.queryForList("select * from async_log_car_apply where sync_count > 0 and type=1 order by id desc limit 0,1");
		if(asyncList.size() > 0) {
			Map<String,Object> map = asyncList.get(0);
			if(map != null && map.get("fkey_end") != null) {
				where = " where fk_id >  " + map.get("fkey_end") + " ";
			}
		}
		
		
		Date start = new Date();
		int count = 0;
		String startId = null;
		String endId = null;
		
		
		List<Map<String,Object>> list =  this.jdbcTemplateCarEnter.queryForList("select * from ( select * from  tp.F_20230508154713_875    "+ where +" order by fk_id asc ) where rownum <=1000 ");
		
		
		try {		
			for(Map<String,Object> item:list) {
				if(startId == null) startId = item.get("fk_id").toString();
				endId =   item.get("fk_id").toString();				
				insert(item);
				count++;
			}
			
		}catch (Exception e) {
			System.out.println("asyncBusApply:"+e.getMessage());
		}finally {
			Date end = new Date();
			long seconds = (end.getTime() - start.getTime())/1000;
			
			if(count > 0) {
				//插入数据库记录
				this.jdbcTemplateDataAi.update(
						"insert into async_log_car_apply(task_begin,task_end,sync_count,fkey_start,fkey_end,duration,type) values(?,?,?,?,?,?,?)",
						start,end,count,startId,endId,seconds,1);
			}
		
			
			
		}
		return count;
		
	}

	private void insert(Map<String, Object> item) {
		try {
			
			this.jdbcTemplateDataAi.update("insert into car_enter_apply(form_id,fkey_id,XNDJDW,DJFZR,DJFZRDH,CRXSY,CSRYLB_TEXT,"
					+ "CSRYLB,CSRYSL,CPH,JSYXM,JSYDH,TXRQ,XQXM,"
					+ "TFDD,YHXY,ZYSX1,ZYSX2,ZYSX3,ZYSX4,ZYSX5,ZYSX6,"
					+ "CN,TXRQ_END,TXRQ_START,apply_type) "
					+ " values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) ",
					item.get("PK_ID"),item.get("FK_ID"),item.get("XNDJDW"),item.get("DJFZR"),item.get("DJFZRDH"),item.get("CRXSY"),item.get("CSRYLB_TEXT"),
					item.get("CSRYLB"),item.get("CSRYSL"),item.get("CPH"),item.get("JSYXM"),item.get("JSYDH"),item.get("TXRQ"),item.get("XQXM"),
					item.get("TFDD"),item.get("YHXY"),item.get("ZYSX1"),item.get("ZYSX2"),item.get("ZYSX3"),item.get("ZYSX4"),item.get("ZYSX5"),item.get("ZYSX6"),
					item.get("CN"),item.get("TXRQ_END"),item.get("TXRQ_START"),1);
		}catch (Exception e) {
			e.printStackTrace();
		}		
	}
	
	
	/**
	 * 同步学校公车
	 */
	public synchronized int asyncSchoolCarApply() {
		//查询条件
		String where = "";
		
		//首先获取最近的同步信息
		List<Map<String,Object>> asyncList = this.jdbcTemplateDataAi.queryForList("select * from async_log_car_apply where sync_count > 0 and type=2 order by id desc limit 0,1");
		if(asyncList.size() > 0) {
			Map<String,Object> map = asyncList.get(0);
			if(map != null && map.get("fkey_end") != null) {
				where = " where fk_id >  " + map.get("fkey_end") + " ";
			}
		}
		
		
		Date start = new Date();
		int count = 0;
		String startId = null;
		String endId = null;
		
		
		List<Map<String,Object>> list =  this.jdbcTemplateCarEnter.queryForList("select * from ( select * from  tp.F_20220706162256_618    "+ where +" order by fk_id asc ) where rownum <=1000 ");
				
		try {		
			for(Map<String,Object> item:list) {
				if(startId == null) startId = item.get("fk_id").toString();
				endId =   item.get("fk_id").toString();				
				insertSchoolCar(item);
				count++;
			}
			
		}catch (Exception e) {
			e.printStackTrace();
		}finally {
			Date end = new Date();
			long seconds = (end.getTime() - start.getTime())/1000;
			
			if(count > 0) {
			//插入数据库记录
			this.jdbcTemplateDataAi.update(
					"insert into async_log_car_apply(task_begin,task_end,sync_count,fkey_start,fkey_end,duration,type) values(?,?,?,?,?,?,?)",
					start,end,count,startId,endId,seconds,2);
			}
			
			
		}
		return count;
		
	}
	
	private void insertSchoolCar(Map<String, Object> item) {
		try {
			
			this.jdbcTemplateDataAi.update("insert into car_enter_apply(form_id,fkey_id,XM,XB,GZH,DW,LXFS,"
					+ "CPHM1,CPHM1SYR,CPHM2,CLHM2SYR,CPHM3,CPHM3SYR,CPHM3XSZ,"
					+ "CPHM4,CPHM4SYR,CPHM5,CPHM5SYR,"
					+ "ZYSX1,ZYSX2,ZYSX3,ZYSX4,ZYSX5,ZYSX6,"
					+ "CN,apply_type) "
					+ " values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) ",
					item.get("PK_ID"),item.get("FK_ID"),item.get("XM"),item.get("XB"),item.get("GZH"),item.get("DW"),item.get("LXFS"),
					item.get("CPHM1"),item.get("CPHM1SYR"),item.get("CPHM2"),item.get("CLHM2SYR"),item.get("CPHM3"),item.get("CPHM3SYR"),item.get("CPHM3XSZ"),
					item.get("CPHM4"),item.get("CPHM4SYR"),item.get("CPHM5"),item.get("CPHM5SYR"),
					item.get("ZYSX1"),item.get("ZYSX2"),item.get("ZYSX3"),item.get("ZYSX4"),item.get("ZYSX5"),item.get("ZYSX6"),
					item.get("CN"),2);
		}catch (Exception e) {
			e.printStackTrace();
		}		
	}
	
	/**
	 * 同步学生车辆
	 */
	public synchronized int asyncStudentCarApply() {
		//查询条件
		String where = "";
		
		//首先获取最近的同步信息
		List<Map<String,Object>> asyncList = this.jdbcTemplateDataAi.queryForList("select * from async_log_car_apply where sync_count > 0 and type=3 order by id desc limit 0,1");
		if(asyncList.size() > 0) {
			Map<String,Object> map = asyncList.get(0);
			if(map != null && map.get("fkey_end") != null) {
				where = " where fk_id >  " + map.get("fkey_end") + " ";
			}
		}
		
		
		Date start = new Date();
		int count = 0;
		String startId = null;
		String endId = null;
		
		
		List<Map<String,Object>> list =  this.jdbcTemplateCarEnter.queryForList("select * from ( select * from  tp.F_20220706165125_331     "+ where +" order by fk_id asc ) where rownum <=1000 ");
				
		try {		
			for(Map<String,Object> item:list) {
				if(startId == null) startId = item.get("fk_id").toString();
				endId =   item.get("fk_id").toString();				
				insertStudentCar(item);
				count++;
			}
			
		}catch (Exception e) {
			e.printStackTrace();
		}finally {
			Date end = new Date();
			long seconds = (end.getTime() - start.getTime())/1000;
			
			if(count > 0) {
			//插入数据库记录
			this.jdbcTemplateDataAi.update(
					"insert into async_log_car_apply(task_begin,task_end,sync_count,fkey_start,fkey_end,duration,type) values(?,?,?,?,?,?,?)",
					start,end,count,startId,endId,seconds,3);
			}
			
			
		}
		return count;
		
	}
	

	private void insertStudentCar(Map<String, Object> item) {
		try {
			
			this.jdbcTemplateDataAi.update("insert into car_enter_apply(form_id,fkey_id,XM,XB,XH,XY,LXFS,"
					+ "CPHM,CLSYRGX_TEXT,CLSYRGX,CLSYRXM,CLSYRLXDH,"
					+ "ZYSX1,ZYSX2,ZYSX3,ZYSX4,ZYSX5,ZYSX6,"
					+ "CN,apply_type) "
					+ " values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) ",
					item.get("PK_ID"),item.get("FK_ID"),item.get("XM"),item.get("XB"),item.get("XH"),item.get("XY"),item.get("LXFS"),
					item.get("CPHM"),item.get("CLSYRGX_TEXT"),item.get("CLSYRGX"),item.get("CLSYRXM"),item.get("CLSYRLXDH"),					
					item.get("ZYSX1"),item.get("ZYSX2"),item.get("ZYSX3"),item.get("ZYSX4"),item.get("ZYSX5"),item.get("ZYSX6"),
					item.get("CN"),3);
		}catch (Exception e) {
			e.printStackTrace();
		}		
	}
	
	

	/**
	 * 同步学生车辆
	 */
	public synchronized int asyncTeacherApply() {
		//查询条件
		String where = "";
		
		//首先获取最近的同步信息
		List<Map<String,Object>> asyncList = this.jdbcTemplateDataAi.queryForList("select * from async_log_car_apply where sync_count > 0 and type=4 order by id desc limit 0,1");
		if(asyncList.size() > 0) {
			Map<String,Object> map = asyncList.get(0);
			if(map != null && map.get("fkey_end") != null) {
				where = " where fk_id >  " + map.get("fkey_end") + " ";
			}
		}
		
		
		Date start = new Date();
		int count = 0;
		String startId = null;
		String endId = null;
		
		
		List<Map<String,Object>> list =  this.jdbcTemplateCarEnter.queryForList("select * from ( select * from  tp.F_20220706151644_187     "+ where +" order by fk_id asc ) where rownum <=1000 ");
				
		try {		
			for(Map<String,Object> item:list) {
				if(startId == null) startId = item.get("fk_id").toString();
				endId =   item.get("fk_id").toString();				
				insertTeacherCar(item);
				count++;
			}
			
		}catch (Exception e) {
			e.printStackTrace();
		}finally {
			Date end = new Date();
			long seconds = (end.getTime() - start.getTime())/1000;
			
			if(count > 0) {
			//插入数据库记录
			this.jdbcTemplateDataAi.update(
					"insert into async_log_car_apply(task_begin,task_end,sync_count,fkey_start,fkey_end,duration,type) values(?,?,?,?,?,?,?)",
					start,end,count,startId,endId,seconds,4);
			}
			
			
		}
		return count;
		
	}
	

	private void insertTeacherCar(Map<String, Object> item) {
		try {
			
			this.jdbcTemplateDataAi.update("insert into car_enter_apply(form_id,fkey_id,XM,XB,GZH,DW,LXFS,"
					+ "CPHM1,CPHM1GX_TEXT,CPHM1GX,CPHM1XM,CPHM1LXFS,"
					+ "CPHM2,CPHM2GX_TEXT,CPHM2GX,CPHM2XM,CPHM2LXDH,"
					+ "ZYSX1,ZYSX2,ZYSX3,ZYSX4,ZYSX5,ZYSX6,"
					+ "CN,ZZCN,apply_type) "
					+ " values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) ",
					item.get("PK_ID"),item.get("FK_ID"),item.get("XM"),item.get("XB"),item.get("GZG"),item.get("DW"),item.get("LXFS"),
					item.get("CPHM1"),item.get("CPHM1GX_TEXT"),item.get("CPHM1GX"),item.get("CPHM1XM"),item.get("CPHM1LXFS"),	
					item.get("CPHM2"),item.get("CPHM2GX_TEXT"),item.get("CPHM2GX"),item.get("CPHM2XM"),item.get("CPHM2LXDH"),
					item.get("ZYSX1"),item.get("ZYSX2"),item.get("ZYSX3"),item.get("ZYSX4"),item.get("ZYSX5"),item.get("ZYSX6"),
					item.get("CN"),item.get("ZZCN"),4);
		}catch (Exception e) {
			e.printStackTrace();
		}		
	}
	
	/**
	 * 同步状态
	 */
	public void asyncApplyState() {
		 String keyId = doAsyncApplyState(null);
		 while(keyId != null) {
			 doAsyncApplyState(keyId);
		 }
	}
	
	private String doAsyncApplyState(String fkeyId) {
		/**
		 * 
		 */
		
		//获取所有不等于4的记录，然后更新即可
		String where = "";
		Object[] params = new Object[] {};
		
		
		if(fkeyId != null) {
			where = " and  fkey_id > ? ";
			params = new Object[] {fkeyId};
		}
		
		String lastKeyId = null;
		
		try {
			String sql = "select id,fkey_id  from car_enter_apply    where apply_status <> 4 "+where+" order by fkey_id asc  limit 0,1000 ";
			List<Map<String,Object>> list =  this.jdbcTemplateDataAi.queryForList(sql,params);
			for(Map<String,Object> map :list) {
				Long id = new Long(map.get("id").toString());
				lastKeyId = (String) map.get("fkey_id");
				
				//更新状态
				List<Map<String,Object>> stateList =this.jdbcTemplateCarEnter.queryForList("select CURRENT_STATE from TP.RT_PROCESSINSTANCE  where PROC_INSTANCE_ID = ?",lastKeyId);
				if(stateList.size() > 0) {
					Map<String,Object> state = stateList.get(0);
					this.jdbcTemplateDataAi.update("update car_enter_apply set apply_status = ? where id = ?",state.get("CURRENT_STATE"),id);
				}
			}
			
		}catch (Exception e) {
			e.printStackTrace();
			//更新报错，返回null
			return null;
		}
		
		return lastKeyId;
		
		
	}
	
	
}
